/*
 * Word Utility
 * to search Korean, English s
 */
package com.ahnchan.ContactsPlus.util;

import java.util.StringTokenizer;

import com.ahnchan.ContactsPlus.ContactsPlusSetting;

/**
 * 
 */
public class WordUtils {

	public static final int HANGUL_BEGIN_UNICODE = 44032; // 
	public static final int HANGUL_END_UNICODE = 55203; //
	public static final int HANGUL_BASE_UNIT = 588;

	public static final int[] INITIAL_SOUND_UNICODE = { 12593, 12594, 12596,
			12599, 12600, 12601, 12609, 12610, 12611, 12613, 12614, 12615,
			12616, 12617, 12618, 12619, 12620, 12621, 12622 };

	public static final char[] INITIAL_SOUND = { 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 
		'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' };

	private ContactsPlusSetting setting = ContactsPlusSetting.instatnce(); 
	
	public static char getFirstInitial(String value, boolean merge, boolean symbol, boolean number) {
		
		String result = "";
		
		if (null == value) return ' ';
		if ("".equals(value)) return ' ';
		
		result = getHangulInitialSound(value.trim());
		
		char cFirst = result.charAt(0);
		
//		setting.
//		if (cFirst < 'A') {
//			cFirst = '#';
//		}
		
		// Merge, Symbol, Number
		if (merge) {
			if (cFirst < 'A') {
				cFirst = '#';
			}
		} else {
			// Number spread
			if (!number) {
				if ((cFirst >= '0') && (cFirst <='9')) {
					cFirst = '0';
					return cFirst;
				}
			}
			
			// Symbol spread
			if (!symbol) {
				if ((cFirst <'A') && !((cFirst >= '0') && (cFirst <='9'))) {
					cFirst = '#';
					return cFirst;
				}
			}
		}
		
		return cFirst;
	}	

	public static boolean isInitialSound(String value, String search) {

		boolean bResult = false;
		String strSound = "";
		
		// search more fast english name. first initial
		StringTokenizer strToken = new StringTokenizer(value);
		while (strToken.hasMoreTokens()) {
			strSound += strToken.nextToken().toUpperCase().substring(0,1);
		}
		
		if (strSound.indexOf(search.toUpperCase()) >= 0) {
			bResult = true;
		}
		
		return bResult;
	}
	
	public static String getIS(String value) {
		String str = "";
		
		StringTokenizer strToken = new StringTokenizer(value);
		while (strToken.hasMoreTokens()) {
			str += strToken.nextToken().toUpperCase().substring(0,1);
		}
		
		return str;
	}
	
	public static String getHIS(String value) {
		
		return getHangulInitialSound(value).toUpperCase();
		
	}
	
	public static boolean isHangulInitialSound(String value, String search) {
		
		boolean bResult = false;
		
		String strSound = getHangulInitialSound(value).toUpperCase();
		
		if (strSound.indexOf(search.toUpperCase()) >= 0) {
			bResult = true;
		}

		return bResult;
	
	}
	
	public static String getHangulInitialSound(String value, String searchKeyword) {
		return getHangulInitialSound(value, getIsChoSungList(searchKeyword));
	}

	public static String getHangulInitialSound(String value, boolean[] isChoList) {

		StringBuffer result = new StringBuffer();

		int[] unicodeList = convertStringToUnicode(value);
		for (int idx = 0; idx < unicodeList.length; idx++) {
			int unicode = unicodeList[idx];

			if (isChoList != null && idx <= (isChoList.length - 1)) {
				if (isChoList[idx]) {
					if (HANGUL_BEGIN_UNICODE <= unicode	&& unicode <= HANGUL_END_UNICODE) {
						int tmp = (unicode - HANGUL_BEGIN_UNICODE);
						int index = tmp / HANGUL_BASE_UNIT;
						result.append(INITIAL_SOUND[index]);
					} else {
						result.append(convertUnicodeToChar(unicode));
					}
				} else {
					result.append(convertUnicodeToChar(unicode));
				}
			} else {
				result.append(convertUnicodeToChar(unicode));
			}
		}

		return result.toString();
	}

	
	private static String toHexString(int decimal) {
		Long intDec = Long.valueOf(decimal);
		return Long.toHexString(intDec);
	}

	/**
	 * Convert char to unicode 
	 * 
	 * @param ch
	 * @return
	 */
	public static int convertCharToUnicode(char ch) {
		return (int) ch;
	}

	/**
	 *
	 * 
	 * @param str
	 * @return
	 */
	public static int[] convertStringToUnicode(String str) {

		int[] unicodeList = null;

		if (str != null) {
			unicodeList = new int[str.length()];
			for (int i = 0; i < str.length(); i++) {
				unicodeList[i] = convertCharToUnicode(str.charAt(i));
			}
		}

		return unicodeList;
	}

	/**
	 * 
	 * 
	 * @param hexUnicode
	 * @return
	 */
	public static char convertUnicodeToChar(String hexUnicode) {
		return (char) Integer.parseInt(hexUnicode, 16);
	}

	/**
	 * 
	 * 
	 * @param unicode
	 * @return
	 */
	public static char convertUnicodeToChar(int unicode) {
		return convertUnicodeToChar(toHexString(unicode));
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	public static String getHangulInitialSound(String value) {

		StringBuffer result = new StringBuffer();

		int[] unicodeList = convertStringToUnicode(value);
		for (int unicode : unicodeList) {

			if (HANGUL_BEGIN_UNICODE <= unicode
					&& unicode <= HANGUL_END_UNICODE) {
				int tmp = (unicode - HANGUL_BEGIN_UNICODE);
				int index = tmp / HANGUL_BASE_UNIT;
				result.append(INITIAL_SOUND[index]);
			} else {
				result.append(convertUnicodeToChar(unicode));

			}
		}

		return result.toString();
	}

	public static boolean[] getIsChoSungList(String name) {
		if (name == null) {
			return null;
		}

		boolean[] choList = new boolean[name.length()];

		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);

			boolean isCho = false;
			for (char cho : INITIAL_SOUND) {
				if (c == cho) {
					isCho = true;
					break;
				}
			}

			choList[i] = isCho;

		}

		return choList;
	}

	public static String checkSearchWord(String str) {
		
		String strResult = "";
		
		// Replace character ' --> it's make error.
		strResult =  str.replaceAll("'", "''s");
		
		return strResult;
	}
}
